package sumSinExecutor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class SumSinExecute {
	private int threadNumber;
	private long n;

	public SumSinExecute(long n, int t) {
		this.n = n;
		this.threadNumber = t;

	}

	private List<Future<Double>> computeSin() {

		List<Future<Double>> list = new ArrayList<Future<Double>>();

		ExecutorService exec = Executors.newFixedThreadPool(threadNumber);

		for (long i = (-1) * n; i <= n; i++) {

			Callable<Double> task = new SumSinCallable(i);
			list.add(exec.submit(task));
		}
		exec.shutdown();
		return list;
	}

	public double sumListOfSin() {
		List<Future<Double>> list = computeSin();
		double sum = 0;

		for (Future<Double> future : list) {
			try {
				sum += future.get();
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (ExecutionException e) {
				e.printStackTrace();
			}
		}

		return sum;
	}

}
